<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;

use App\Models\Article;
use App\Services\ArticleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ArticleController extends AdminController
{
    //添加
    public function  add(Request $request){

        $validator = Validator::make($request->all(), [
            'article_categories_id' => 'required|integer|digits_between:1,20|min:1',
            'title' => 'required|string',
            'recommend' => 'required|integer',
            'top' => 'required|integer',
            'status' => 'required|integer',


        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        Article::create($request->all());
        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'article_categories_id' => 'required|integer|digits_between:1,20|min:1',
            'title' => 'required|string',
            'recommend' => 'required|integer',
            'top' => 'required|integer',
            'status' => 'required|integer',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token']);
        $article = new Article();
        $article->find($request->id)->update($putData);
        return $this->success();
    }
    // 上传
    public function upfile(Request $request) {
        $url = config('filesystems.disks.article.url');
        $default = config('filesystems.disks.article.default');
        $pic =  ArticleService::getInstance()->upfile($request,$url,$default,'article');
        return $this->success([
                'pic'=>$pic,
            ]
        );
    }

    public function  info(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $info =  Article::find($request->id);
        return $this->success($info);
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
            'article_categories_id' => 'integer|digits_between:1,20|min:1',
            'title' => 'string',
            'recommend' => 'integer',
            'top' => 'integer',
            'status' => 'integer',


        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $article_categories_id = $request->input('article_categories_id');
        $title = $request->input('title');
        $recommend = $request->input('recommend');
        $top = $request->input('top');
        $status = $request->input('status');


        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','desc');

        $list = ArticleService::getInstance()->list($page,$limit,$sort,$order, $article_categories_id,$title,$recommend,$top,$status);
        return $this->success($list);
    }
    //全部
    public function  listAll(Request $request){
        $validator = Validator::make($request->all(), [
            'top' => 'integer',
            'article_categories_id' => 'integer|digits_between:1,20|min:1',
            'recommend' => 'integer',
            'name' => 'string',
            'status' => 'integer',


        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $article_categories_id = $request->input('article_categories_id');
        $top = $request->input('top');
        $recommend= $request->input('recommend');
        $status = $request->input('status');
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','desc');

        $listAll = ArticleService::getInstance()->listAll($name,$top,$recommend,$status,$article_categories_id,$sort,$order);
        return $this->success($listAll);
    }
    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $slide = Article::find($request->id);
        $slide->delete();
        return $this->success();
    }

    //增加浏览量
    public function  addPV(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $article = Article::find($request->id);
        $pv = $article->pv+1;
        $article= new Article();
        $article->find($request->id)->update(['pv'=>$pv]);
        return $this->success();
    }
}
